package org.artifact.core.context.session;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import org.artifact.core.context.packet.NetworkPacket;

import java.net.SocketAddress;

public class NettySession extends AbstractSession{
    /** Netty Channel */
    private Channel channel = null;

    public NettySession(Channel channel) {
		super();
		this.channel = channel;
	}

	@Override
    public String getId() {
        return channel.id().asLongText();
    }

    @Override
    public SocketAddress localAddress() {
        return channel.localAddress();
    }

    @Override
    public SocketAddress remoteAddress() {
        return channel.remoteAddress();
    }

    @Override
    public void close() {
        channel.close();
    }

    @Override
    public boolean isOpen() {
        return channel.isOpen();
    }

    @Override
    public boolean isActive() {
        return channel.isActive();
    }

    @Override
    public boolean isWritable() {
        return channel.isWritable();
    }

    @Override
    public void send(NetworkPacket packet) {
        channel.writeAndFlush(packet);
    }

    @Override
    public void sendAndClose(NetworkPacket packet) {
        channel.writeAndFlush(packet).addListener(ChannelFutureListener.CLOSE);
    }

    @Override
    public void receive(NetworkPacket packet) {

    }

	@Override
	public void send(byte[] data) {
		channel.writeAndFlush(data);
	}

	@Override
	public void sendAndClose(byte[] data) {
		channel.writeAndFlush(data).addListener(ChannelFutureListener.CLOSE);
	}
}
